//
//  ViewController.m
//  FlatThrowMotionCalculator
//
//  Created by Apple on 2018/12/20.
//  Copyright © 2018 lichang1220. All rights reserved.
//

#import "ViewController.h"
#import "Views/HWDownSelectedView.h"
#import "Views/UnderLineTextField.h"
#import <MBProgressHUD.h>
#import <Masonry.h>
#import <BRPickerView.h>


#define UIColorFromRGB(rgbValue) \
[UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]



@interface ViewController ()<HWDownSelectedViewDelegate>
@property (weak, nonatomic) IBOutlet HWDownSelectedView *selectCalcWhat;
@property (weak, nonatomic) IBOutlet UITextField *hSpeed;
@property (weak, nonatomic) IBOutlet UITextField *time;
@property (weak, nonatomic) IBOutlet UITextField *hDistance;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    [[UITextField appearance] setTintColor:UIColorFromRGB(0xEC6350)];
    
    _selectCalcWhat.listArray = [NSArray arrayWithObjects:@"Horizontal distance",@"Initial horizontal speed ",@"Time", nil];
    
    _selectCalcWhat.selectedIndex = 0;
    _selectCalcWhat.delegate = self;
    
    self.hDistance.enabled = NO;
    self.hDistance.backgroundColor = UIColorFromRGB(0xF0F2F5);//[UIColor lightGrayColor];
    
    
}

- (IBAction)clickCalc:(id)sender {
    
    if (self.selectCalcWhat.selectedIndex == 0) {
        
        if (self.hSpeed.text.length == 0 || self.time.text.length == 0  ) {
            
            [self ShowHud:@"Please enter value for all the fields"];
            return ;
        }
        
        if (![self isPureNumandCharacters:self.hSpeed.text] || ![self isPureNumandCharacters:self.time.text]) {
            [self ShowHud:@"The value you entered is not valid"];
            return;
        }
        
        CGFloat hs = self.hSpeed.text.floatValue;
        CGFloat t = self.time.text.floatValue;
        
        CGFloat hD = round((hs*t)*100)/100;
        self.hDistance.text = [NSString stringWithFormat:@"%.2f", hD];
        self.hDistance.textColor = UIColorFromRGB(0xEDA733);//[UIColor greenColor];
        
    }else if(self.selectCalcWhat.selectedIndex == 1){
        if (self.hDistance.text.length == 0 || self.time.text.length == 0  ) {
            
            [self ShowHud:@"Please enter value for all the fields"];
            return ;
        }
        
        if (![self isPureNumandCharacters:self.hDistance.text] || ![self isPureNumandCharacters:self.time.text]) {
            [self ShowHud:@"The value you entered is not valid"];
            return;
        }
        
        CGFloat hD = self.hDistance.text.floatValue;
        CGFloat t = self.time.text.floatValue;
        
        CGFloat hs = round((hD/t)*100)/100;
        self.hSpeed.text = [NSString stringWithFormat:@"%.2f", hs];
        self.hSpeed.textColor = UIColorFromRGB(0xEDA733);//[UIColor greenColor];
   
    }else if(self.selectCalcWhat.selectedIndex == 2){
        
        if (self.hSpeed.text.length == 0 || self.hDistance.text.length == 0  ) {
            
            [self ShowHud:@"Please enter value for all the fields"];
            return ;
        }
        
        if (![self isPureNumandCharacters:self.hSpeed.text] || ![self isPureNumandCharacters:self.hDistance.text]) {
            [self ShowHud:@"The value you entered is not valid"];
            return;
        }
        
        CGFloat hs = self.hSpeed.text.floatValue;
        CGFloat hD = self.hDistance.text.floatValue;
        
        CGFloat t = round((hD/hs)*100)/100;
        self.time.text = [NSString stringWithFormat:@"%.2f", t];
        self.time.textColor = UIColorFromRGB(0xEDA733);//[UIColor greenColor];
    }
    
}



- (void)downSelectedView:(HWDownSelectedView *)selectedView didSelectedAtIndex:(NSIndexPath *)indexPath{
    
    if (indexPath.row == 0) {//Horizontal distance
        self.hDistance.enabled = NO;
        self.hDistance.text = @"";
        self.hDistance.backgroundColor = UIColorFromRGB(0xF0F2F5);//
        self.hDistance.textColor = UIColorFromRGB(0xEC6350);
        [self checkResetTextfield:self.hSpeed];
        [self checkResetTextfield:self.time];
        
        
    }else if(indexPath.row == 1){//Initial horizontal speed
        self.hSpeed.enabled = NO;
        self.hSpeed.text = @"";
        self.hSpeed.backgroundColor = UIColorFromRGB(0xF0F2F5);//[UIColor lightGrayColor];
        self.hSpeed.textColor = UIColorFromRGB(0xEC6350);
        [self checkResetTextfield:self.hDistance];
        [self checkResetTextfield:self.time];
        
    }else if(indexPath.row == 2){//Time
        self.time.enabled = NO;
        self.time.text = @"";
        self.time.backgroundColor = UIColorFromRGB(0xF0F2F5);//[UIColor lightGrayColor];
        self.time.textColor = UIColorFromRGB(0xEC6350);
        [self checkResetTextfield:self.hSpeed];
        [self checkResetTextfield:self.hDistance];
    }
    
    
    
}

- (void)checkResetTextfield:(UITextField *)textfield{
    if (textfield.enabled == NO) {
        textfield.enabled = YES;
        textfield.backgroundColor = [UIColor whiteColor];
    }
    
    textfield.textColor = UIColorFromRGB(0xEC6350);
    
}

- (BOOL)isPureNumandCharacters:(NSString *)string
{
    string = [string stringByTrimmingCharactersInSet:[NSCharacterSet decimalDigitCharacterSet]];
    if(string.length > 0 && ![string isEqualToString:@"."]){
        return NO;
    }
    return YES;
}

- (void)ShowHud:(NSString *)hudStr{
    if (hudStr.length == 0) {
        return;
    }
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    hud.label.text = hudStr;
    hud.mode = MBProgressHUDModeText;
    [hud hideAnimated:YES afterDelay:1.3];
}


- (UIStatusBarStyle)preferredStatusBarStyle{
    return UIStatusBarStyleLightContent;
}
@end
